package com.jian.mall.produce.service.impl;

import com.jian.mall.common.utils.PageUtils;
import com.jian.mall.common.utils.Query;
import com.jian.mall.common.utils.R;
import com.jian.mall.produce.dao.BrandDao;
import com.jian.mall.produce.entity.BrandEntity;
import com.jian.mall.produce.entity.CategoryEntity;
import com.jian.mall.produce.service.BrandService;
import com.jian.mall.produce.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jian.mall.produce.dao.CategoryBrandRelationDao;
import com.jian.mall.produce.entity.CategoryBrandRelationEntity;
import com.jian.mall.produce.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;
    @Autowired
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long brandId) {
        if (brandId == 0) {
            IPage<CategoryBrandRelationEntity> page = this.page(
                    new Query<CategoryBrandRelationEntity>().getPage(params),
                    new QueryWrapper<>()
            );
            return new PageUtils(page);
        }
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId)
        );
        return new PageUtils(page);
    }

    @Override
    public R saveById(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        if (brandId == null || catelogId == null)
            return R.error();

        BrandEntity brandEntity = brandService.getById(brandId);
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        if (brandEntity == null || categoryEntity == null)
            return R.error();

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        boolean save = this.save(categoryBrandRelation);
        return save ? R.ok() : R.error();
    }

    @Override
    public void myUpdateById(CategoryBrandRelationEntity categoryBrandRelation) {
        if (categoryBrandRelation.getCatelogName() == null && categoryBrandRelation.getCatelogId() != null) {
            // 需要回显分类名称的修改
            CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }
        baseMapper.updateById(categoryBrandRelation);
    }

    @Override
    public void updateByCategoryIdAndName(Long catId, String name) {
        categoryBrandRelationDao.updateByCatId(catId, name);
    }

    @Override
    public void updateByBrandIdAndName(Long brandId, String name) {
        categoryBrandRelationDao.updateByBrandId(brandId, name);
    }

    @Override
    public List<BrandEntity> selectBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities =
                baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        List<Long> brandIds = relationEntities.stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        if (brandIds.size() <= 0)
            return null;

        return brandDao.selectList(new QueryWrapper<BrandEntity>().in("brand_id", brandIds));
    }

}